#include "LoginScene.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CCSGUIReader.h"
#include "../Networks/NetworkProtocolMud.h"
#include "../Networks/NetworkManager.h"
#include "ServerSelectScene.h"

cocos2d::Scene* LoginScene::scene()
{
	// 'scene' is an autorelease object
	cocos2d::Scene* scene = cocos2d::Scene::create();

	// 'layer' is an autorelease object
	LoginScene *layer = LoginScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool LoginScene::init()
{
	cocos2d::FileUtils::getInstance()->addSearchPath("../../Resources");

	m_pAccountText = 0;
	m_pPasswordText = 0;
	m_pCurrentText = 0;
	NetworkManager::getSingleton()->createSocket("203.70.8.198", 4721, LOGIN_SOCKET_TAG, new NetworkProtocolMud());

	if ( !Layer::init() )
	{
		return false;
	}

	cocos2d::ui::Widget* rootLayout = (cocostudio::GUIReader::getInstance()->widgetFromJsonFile("LoginUI.ExportJson"));
	if (!rootLayout) return false;
	cocos2d::ui::Widget* loginPanel = rootLayout->getChildByName("LoginPanel");
	if (loginPanel)
	{
		cocos2d::ui::Button* closeButton = dynamic_cast<cocos2d::ui::Button*>(loginPanel->getChildByName("CloseButton"));
		closeButton->addTouchEventListener(this, (cocos2d::ui::SEL_TouchEvent)&LoginScene::eventCloseButton);
		m_pAccountText = dynamic_cast<cocos2d::ui::TextField*>(loginPanel->getChildByName("AccountText"));
		m_pAccountText->addEventListenerTextField(this, (cocos2d::ui::SEL_TextFieldEvent)&LoginScene::eventAccountText);
		m_pPasswordText = dynamic_cast<cocos2d::ui::TextField*>(loginPanel->getChildByName("PasswordText"));
		m_pPasswordText->addEventListenerTextField(this, (cocos2d::ui::SEL_TextFieldEvent)&LoginScene::eventPasswordText);
		cocos2d::ui::Button* loginButton = dynamic_cast<cocos2d::ui::Button*>(loginPanel->getChildByName("LoginButton"));
		loginButton->addTouchEventListener(this, (cocos2d::ui::SEL_TouchEvent)&LoginScene::eventLoginButton);
	}
#ifdef WIN32
	cocos2d::EventListenerKeyboard* listenerKeyboard = cocos2d::EventListenerKeyboard::create();
	listenerKeyboard->onKeyPressed = CC_CALLBACK_2(LoginScene::eventKeyboardPressed, this);
	cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerKeyboard, this);
#endif
	addChild(rootLayout);  
	
	return true;
}

void LoginScene::eventCloseButton(cocos2d::Ref* sender, cocos2d::ui::TouchEventType type)
{
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_BEGAN:
		cocos2d::Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
		exit(0);
#endif
		break;
	default:
		break;
	}
}

void LoginScene::eventLoginButton(cocos2d::Ref* sender, cocos2d::ui::TouchEventType type)
{
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		doLogin();
		break;
	default:
		break;
	}
}

void LoginScene::eventAccountText(cocos2d::Ref* sender, cocos2d::ui::TextFiledEventType type)
{
	switch (type)
	{
	case cocos2d::ui::TEXTFIELD_EVENT_ATTACH_WITH_IME:
		{
			m_pCurrentText = m_pAccountText;
		}
		break;
	case  cocos2d::ui::TEXTFIELD_EVENT_DETACH_WITH_IME:
		m_pCurrentText = 0;
		break;
	case cocos2d::ui::TEXTFIELD_EVENT_INSERT_TEXT:
		break;
	case cocos2d::ui::TEXTFIELD_EVENT_DELETE_BACKWARD:
		break;
	default:
		break;
	}

}

void LoginScene::eventPasswordText(cocos2d::Ref* sender, cocos2d::ui::TextFiledEventType type)
{
	switch (type)
	{
	case cocos2d::ui::TEXTFIELD_EVENT_ATTACH_WITH_IME:
		m_pCurrentText = m_pPasswordText;
		break;
	case  cocos2d::ui::TEXTFIELD_EVENT_DETACH_WITH_IME:
		m_pCurrentText = 0;
		break;
	case cocos2d::ui::TEXTFIELD_EVENT_INSERT_TEXT:
		break;
	case cocos2d::ui::TEXTFIELD_EVENT_DELETE_BACKWARD:
		break;
	default:
		break;
	}

}

void LoginScene::eventKeyboardPressed(cocos2d::EventKeyboard::KeyCode key, cocos2d::Event* ev)
{
#ifdef WIN32
	if (key == cocos2d::EventKeyboard::KeyCode::KEY_BACKSPACE)
	{
		cocos2d::IMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
		/*if (m_pCurrentText)
		{
			m_pCurrentText->setDeleteBackward(true);
		}*/
	}
#endif
}

void LoginScene::doLogin()
{
	if ((!m_pAccountText) || (!m_pPasswordText)) return;
	NetworkManager::getSingleton()->callRemoteCommand(LOGIN_SOCKET_TAG, MUD_LLOGIN, 
		m_pAccountText->getStringValue().c_str(), m_pPasswordText->getStringValue().c_str());
	cocos2d::Scene* selServerScene = ServerSelectScene::scene();
	cocos2d::Director::getInstance()->replaceScene(selServerScene);
}